package Security;
import java.awt.Point;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class GenerateDynamicAuthenticationKey {
	
	public static void main(String[] args){
		GenerateDynamicAuthenticationKey te = new GenerateDynamicAuthenticationKey();
		if(te.isDynamicAutenticationKeyValid(te.genereteOneDynamicAuthenticationKyes())){
			System.out.println("Authentication Key == "+te.genereteOneDynamicAuthenticationKyes());
		}
	}
	
	public String genereteOneDynamicAuthenticationKyes() {
		String sessionKey = getSessionKey(getGMTGeneratedTTimes());
		return sessionKey;
	}
	
	public boolean isDynamicAutenticationKeyValid(String key) {
		String[] tarik = generete5AuthenticationKyes();
		for (int i = 0; i < tarik.length; i++) {
			if (key.equals(tarik[i])) {
				return true;
			}
		}
		return false;
	}
	
	private int getCurrentMinutes() {
		SimpleDateFormat sdf = new SimpleDateFormat("mm");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
		Date date = date = new Date();
		if (Integer.parseInt(sdf.format(date)) == 60) {
			return 0;
		}
		if (Character.getNumericValue(sdf.format(date).toString().charAt(0)) == 0) {
			return Character.getNumericValue(sdf.format(date).toString()
					.charAt(1));
		}
		return Integer.parseInt(sdf.format(date));
	}

	private String getSessionKey(int[] yMDhm) {
		String keyCreator = getKeyCreator(yMDhm[0], yMDhm[1], yMDhm[2],
				yMDhm[3], yMDhm[4]);
		ArrayList keys = getSecretMultipleKeys(keyCreator);
		String authenticationKey = getMixedMultiplesKeys(keys);
		return authenticationKey;
	}

	private String getKeyCreator(int year, int month, int day, int h, int min) {
		int months[] = {0,849245601, 781235823, 758293013, 849304274, 718381924,
				748132040, 823331946, 728503925, 849132005, 389123234,
				753928492, 785829488 };
		int dayNbr[] = {0,8578492, 3456723, 1740254, 9843235, 3245786, 65234687,
				8343145, 6247916, 6426904, 4313579, 8712450, 5050127, 4719503,
				70332467, 8292043, 9602106, 9731207, 9631456, 4336789, 4234567,
				5553190, 4214676, 4836342, 7423794, 2479642, 9790425, 5316896,
				2468962, 6365843, 3809326, 4441085 };
		int hours[] = { 415685, 468073, 247932, 589723, 235807, 346909, 235708,
				357431, 468907, 258796, 346832, 925189, 163890, 643792, 589032,
				657832, 451678, 536896, 542578, 367962, 357378, 175378, 862790,
				134675 };
		int minutes[] = { 874, 743, 822, 982, 913, 129, 432, 752, 569, 255,
				633, 177, 882, 834, 122, 654, 633, 213, 298, 452, 312, 939,
				431, 976, 753, 872, 137, 876, 886, 299, 876, 119, 399, 137,
				198, 268, 357, 832, 533, 782, 532, 790, 128, 467, 368, 270,
				964, 233, 765, 657, 257, 224, 766, 146, 934, 366, 357, 244,
				672, 985 };
		int calc1 = 0, calc2 = 0, calc3 = 0;
		try {
			calc1 = ((dayNbr[day] * ((months[month]) * year * minutes[min] / 2)) * (minutes[min]/ 2 * year + dayNbr[day]))/ (dayNbr[day] / hours[h] * minutes[min] * year / minutes[min] / 2) + (minutes[min] / 2);
			calc2 = (dayNbr[day])* ((hours[h] * months[month] * minutes[min] * 2) / (minutes[min] / 2))	/ minutes[min];
			if (calc1 < 0) {
				calc1 = -1 * (calc1);
			}
			if (calc2 < 0) {
				calc2 = -1 * (calc2);
			}
			if (calc1 > calc2) {
				calc3 = ((calc1 + calc2) * 2) / (minutes[min] / 6);
			}
			if (calc1 < calc2) {
				calc3 = ((calc2 + calc1) * 2) / (minutes[min] / 7);
			}
			if (calc3 < 0) {
				calc3 = -1 * (calc3);
			}

		} catch (Exception ex) {
			System.out.println("\n\n\n\n\n############################################################################################################################################################");
			System.out.println("ERROR has accured in:  -- getKeyCreator() -- ");
			System.out.println("############################################################################################################################################################");
			System.out.println("calc1=((dayNbr[" + day + "]*((months[" + month
					+ "])*year+minutes[" + min + "]*2))*(minutes[" + min
					+ "]/2*year+dayNbr[" + day + "] ))/(dayNbr[" + day
					+ "]/hours[" + h + "]*minutes[" + min + "]*year/minutes["
					+ min + "]/2)+(minutes[" + min + "]/2);");
			System.out.println("calc2=(dayNbr[" + day + "])*((hours[" + h
					+ "]*months[" + month + "]*minutes[" + min
					+ "]*2)/(minutes[" + min + "]/2))/minutes[" + min + "];");
			ex.printStackTrace();
			System.out.println("(months[" + month + "]  : months.length == "
					+ months.length);
			System.out.println("dayNbr[" + day + "] : dayNbr.length == "
					+ dayNbr.length);
			System.out.println("hours[" + h + "] : hours.length == "
					+ hours.length);
			System.out.println("minutes[" + min + "] : minutes.length"
					+ minutes.length);
			if (calc1 > calc2) {
				System.out.println("calc3=(calc1-calc2)/minutes[" + min + "];");
			} else {
				System.out.println("calc3=(calc2-calc1)/minutes[" + min + "];");
			}
			System.out
					.println("############################################################################################################################################################\n\n\n\n\n");
		}
		return String.valueOf(calc3);
	}

	private ArrayList getSecretMultipleKeys(String keyCreator) {
		String alphabetCode[] = { "1b5gfh4", "a6qv6bw", "gaml7c1", "meav52l",
				"plar42t", "yrtml59", "1ktx4b1", "lkbe49v", "56vstmp",
				"c7w6ma4", "y9lsr59", "lm4rts3", "q1cpm56", "6qml45s",
				"7tk2ca5", "o8av4mt", "m1il4js", "u5nalff", "hu3sd52",
				"8kf1aop", "468kspv", "75mso4a", "5smf45g", "ipskv48",
				"pl1ms8ed", "14idc45" };
		int increment = 2;
		int i = 0;
		ArrayList keys = new ArrayList();
		try {
			for (i = 0; i < keyCreator.length(); i = i + increment) {
				if (i == keyCreator.length() - 2) {
					increment = 1;
				}
				if (i < keyCreator.length() - 2
						&& (Character.getNumericValue(keyCreator.charAt(i)) + Character
								.getNumericValue(keyCreator.charAt(i + 1))) < 26) {
					keys.add(alphabetCode[Character.getNumericValue(keyCreator
							.charAt(i))]);
					keys.add(alphabetCode[Character.getNumericValue(keyCreator
							.charAt(i + 1))]);
					keys.add(alphabetCode[(Character.getNumericValue(keyCreator
							.charAt(i)) + Character.getNumericValue(keyCreator
							.charAt(i + 1)))]);
				} else if (i < keyCreator.length() - 2
						&& (Character.getNumericValue(keyCreator.charAt(i)) + Character
								.getNumericValue(keyCreator.charAt(i + 1))) > 25) {
					keys.add(alphabetCode[Character.getNumericValue(keyCreator
							.charAt(i))]);
					keys.add(alphabetCode[Character.getNumericValue(keyCreator
							.charAt(i + 1))]);
				} else if (i < 26 && i < keyCreator.length() - 1) {
					keys.add(alphabetCode[Character.getNumericValue(keyCreator
							.charAt(i))]);
				}
			}
		} catch (Exception ex) {
			System.out
					.println("\n\n\n\n\n############################################################################################################################################################");
			System.out
					.println("ERROR has accured in:  -- getSecretMultipleKeys -- ");
			System.out
					.println("############################################################################################################################################################\n\n\n\n\n");
			ex.printStackTrace();
		}
		return keys;
	}

	private String getMixedMultiplesKeys(ArrayList keys) {
		char[] authenticationKey = null;
		int p = 0, q = 0;
		int x = 0, size = 0;
		;
		try {
			for (int z = 0; z < keys.size(); z++) {
				size = size + keys.get(z).toString().length();
			}
			authenticationKey = new char[size];
			for (p = 0; p < keys.get(0).toString().length(); p++) {
				for (q = 0; q < keys.size() && x < authenticationKey.length; q++) {
					authenticationKey[x] = keys.get(q).toString().charAt(p);
					x++;
				}
			}
		} catch (Exception ex) {
			System.out
					.println("\n\n\n\n\n############################################################################################################################################################");
			System.out
					.println("ERROR has accured in:  -- getMixedMultiplesKeys -- ");
			System.out
					.println("############################################################################################################################################################");

			System.out.println("authenticationKey[" + x + "]=keys.get(" + q
					+ ").toString().charAt(" + p + ")\n");
			System.out.println("authenticationKey[" + x
					+ "]   -- authenticationKey.length == "
					+ authenticationKey.length);
			System.out.println("keys.get(" + q + ")  --  keys.size() == "
					+ keys.size());
			System.out.println("keys.get(" + q + ").toString().charAt(" + p
					+ ") --   keys.get(" + q + ").toString().length()"/**
			 * 
			 * 
			 * 
			 * +keys.get(* q * ).toString * (* ).length(* )
			 */
			);
			System.out
					.println("############################################################################################################################################################\n\n\n\n\n");
			ex.printStackTrace();
		}
		return new String(authenticationKey);
	}

	private int[] getCurrentGMTTime() {
		int[] time = new int[5];
		DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
		String tiime = dateFormat.format(cal.getTime());
		System.out.println(tiime);
		int timeIndex = 0, lastSeparator = -1;
		for (int i = 0; i < tiime.length() && timeIndex < time.length; i++) {
			if (tiime.charAt(i) == ':') {
				time[timeIndex] = Integer.parseInt(tiime.substring(
						(lastSeparator + 1), i));
				lastSeparator = i;
				timeIndex++;
			}
		}
		return time;
	}

	private int[] getGMTGeneratedTTimes() {
		int[] time = new int[5];
		DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
		String tiime = dateFormat.format(cal.getTime());
		int timeIndex = 0, lastSeparator = -1;
		for (int i = 0; i < tiime.length() && timeIndex < time.length; i++) {
			if (tiime.charAt(i) == ':') {
				time[timeIndex] = Integer.parseInt(tiime.substring(
						(lastSeparator + 1), i));
				lastSeparator = i;
				timeIndex++;
			}
		}
		return time;
	}

	private int[] addGMTTime(int timeIntervall) {
		int[] time = new int[5];
		DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
		cal.add(Calendar.MINUTE, timeIntervall);
		String tiime = dateFormat.format(cal.getTime());
		int timeIndex = 0, lastSeparator = -1;
		for (int i = 0; i < tiime.length() && timeIndex < time.length; i++) {
			if (tiime.charAt(i) == ':') {
				time[timeIndex] = Integer.parseInt(tiime.substring(
						(lastSeparator + 1), i));
				lastSeparator = i;
				timeIndex++;
			}
		}
		return time;
	}

	private String returnCurrentGMTTime() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd:HH:mm:");
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
		return dateFormat.format(cal.getTime());
	}

	private String[] generete5AuthenticationKyes() {
		int timeIntervall = -1;
		String[] sessionKyes = new String[5];
		for (int i = 0; i < 5; i++) {
			sessionKyes[i] = getSessionKey(addGMTTime(timeIntervall));
			timeIntervall++;
		}

		return sessionKyes;
	}
}